package com.ruoyi.web.controller.school;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.school.domain.PacLearningFilesData;
import com.ruoyi.school.domain.Student;
import com.ruoyi.school.domain.dto.ImportSexOneOneDTO;
import com.ruoyi.school.service.ExcelImportService;
import com.ruoyi.school.service.PacLearningFilesDataService;
import com.ruoyi.school.service.StudentService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/*
 * @Author Lin
 * @Description 学习档案相关接口
 * @Date 2025/6/5
 */
@RestController
@RequestMapping("/school/leaningFileData")
public class PacLeaningFileDataController extends BaseController {

    @Resource
    private ExcelImportService excelImportService;

    @Resource
    private PacLearningFilesDataService pacLearningFilesDataService;

    @Resource
    private StudentService studentService;

    /**
     * 导入数据
     */
    @Log(title = "学习档案数据导入", businessType = BusinessType.INSERT)
    @PostMapping("/importData")
    public AjaxResult importZhiyuData(@RequestParam("file") MultipartFile file,
                                      @RequestParam("type") Integer type
    ) throws IOException {
        List<ImportSexOneOneDTO> importSexOneOneDTOS = excelImportService.importExcel(file);
        importSexOneOneDTOS.forEach(importSexOneOneDTO -> {
            PacLearningFilesData pacLearningFilesData = new PacLearningFilesData();
            pacLearningFilesData.setJsonData(importSexOneOneDTO.getImportText());
            // 根据学号查询
            Student student = new Student();
            student.setStuNum(importSexOneOneDTO.getStuNumber());
            Student selectOne = studentService.selectOne(student);
            if (selectOne != null) {
                pacLearningFilesData.setStuId(selectOne.getId().longValue());
                pacLearningFilesData.setLearningType(type);
                pacLearningFilesDataService.importData(pacLearningFilesData);
            }
        });

        return success();
    }

    /**
     * 查询数据
     */
    @GetMapping("/list")
    public TableDataInfo list(PacLearningFilesData pacLearningFilesData) {
        startPage();
        List<PacLearningFilesData> list = pacLearningFilesDataService.getList(pacLearningFilesData);

        return getDataTable(list);
    }

    /**
     * 获取学生自己的6211成绩
     */
    @GetMapping("/myList")
    public TableDataInfo myList(PacLearningFilesData pacLearningFilesData) {
        startPage();
        List<PacLearningFilesData> list = pacLearningFilesDataService.myList(pacLearningFilesData);

        return getDataTable(list);
    }
}
